package cn.mdmm.controller.file;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.mdmm.controller.BaseController;
import cn.modoumama.common.utils.FileUtil;

/**
 * 类描述：文件上传管理<br>
 * 创建人：邓强   <br>
 * 创建时间：2017年11月1日 上午11:53:20    <br> 
 * 修改人：  <br>
 * 修改时间：2017年11月1日 上午11:53:20   <br>  
 * 修改备注：     <br>
 * @version   V1.0
 */
@Controller
@RequestMapping(value = "/file")
public class FileController extends BaseController {
	Logger log = LoggerFactory.getLogger(getClass());
    
	@RequestMapping(value = "upload")
	@ResponseBody
	public Map<String, Object> upload(HttpServletRequest request,MultipartFile upfile){
		//{"state": "SUCCESS","title": "20170303_1488502564030022030.jpg","original": "joinplan1.jpg","type": ".jpg","url": "/static/upload/file/20170303_1488502564030022030.jpg","size": "38928"}
		Map<String, Object> reMap = new HashMap<>();
		String url = FileUtil.saveImgageFile(upfile);
		if(url != null){
			reMap.put("state", "SUCCESS");
			reMap.put("url", url);
			reMap.put("type", url.replaceAll(".+(\\.\\w{2,3})$", "$1"));
			reMap.put("size", upfile.getSize());
		}else{
			reMap.put("state", "FAILD");
		}
		return reMap;
	}
}
